/*
 * txdnet.cn tonydon
 * by netbeans ide 7.2
 */
package cn.txdnet.txcms.dao;

import cn.txdnet.txcms.domain.Catalogs;
import cn.txdnet.txcms.mapper.CatalogsMapper;
import java.util.List;
import java.util.Map;
import txdnet.util.Logger;

/**
 *
 * @author tangxiaodong
 */
public class CatalogsDAO extends MybatisBaseDAO {

    /**
     * 建立类目
     *
     * @param ct
     * @return
     */
    public boolean insertCatalogs(Catalogs ct) {
        boolean flag = true;
        CatalogsMapper mapper = this.getMapper(CatalogsMapper.class);
        try {
            int new_cid;
            if (mapper.insertCatalogs(ct) > 0) {
                new_cid = ct.getCid();
                //如果是建立子分类
                if (ct.getRid() > 0) {
                    Catalogs ct1 = new Catalogs();
                    ct1.setCid(ct.getRid());
                    ct1.setNodenum(1);
                    flag = mapper.updateNodenum(ct1) > 0 ? true : false;
                    if (!flag) {
                        throw new RuntimeException("Update Catalogs Nodenum Failed.");
                    }
                }
                //更新路径
                if (ct.getCtp() == null || ct.getCtp().equals("0")) {
                    ct.setCtp(Integer.toString(new_cid).concat("-"));
                } else {
                    ct.setCtp(ct.getCtp().concat(Integer.toString(new_cid)).concat("-"));
                }
                // 更新类ID路径
                flag = mapper.updateCtp(ct) > 0 ? true : false;
                if (!flag) {
                    throw new RuntimeException("Make New Catalogs Failed.");
                }
            } else {
                flag = false;
                if (!flag) {
                    throw new RuntimeException("Insert New Catalogs Failed.");
                }
            }
        } catch (Exception e) {
            Logger.error(this.getClass(), e);
        } finally {
            return flag;
        }
    }

    /**
     * 删除终类目
     * @param cid
     * @param rid
     * @return 
     */
    public boolean deleteFinal(int cid, int rid) {
        boolean flag = true;
        CatalogsMapper mapper = this.getMapper(CatalogsMapper.class);
        try {
            // 如果关联id不为0则更新父类节点的NODENUM
            if (rid > 0) {
                Catalogs ct = new Catalogs();
                ct.setCid(rid);
                ct.setNodenum(-1);
                flag = mapper.updateNodenum(ct) > 0 ? true : false;
                if (!flag) {
                    throw new RuntimeException("Update Catalogs Nodenum Error.");
                }
            }
            // 删除分类
            flag = mapper.deleteFinal(cid) > 0 ? true : false;
            if (!flag) {
                throw new RuntimeException("Delete Final Catalogs Error.");
            }
        } catch (Exception e) {
            Logger.error(this.getClass(), e);
        } finally {
            return flag;
        }
    }

    /**
     * 更新类目基本信息
     *
     * @param m
     * @return
     */
    public int updateBase(Map<String, Object> m) {
        CatalogsMapper mapper = this.getMapper(CatalogsMapper.class);
        return mapper.updateBase(m);
    }

    /**
     * 查询类目总数
     *
     * @return
     */
    public int getCatalogsNum() {
        CatalogsMapper mapper = this.getMapper(CatalogsMapper.class);
        return mapper.queryCatalogsCount();
    }

    /**
     * 通过ctp查询关联类目列表
     *
     * @param ctp
     * @return
     */
    public List<Catalogs> listByCtp(String ctp) {
        CatalogsMapper mapper = this.getMapper(CatalogsMapper.class);
        return mapper.listByCtp(ctp);
    }

    /**
     * 查询类目信息
     *
     * @param m
     * @return
     */
    public List<Map> listByFactor(Map<String, Object> m) {
        CatalogsMapper mapper = this.getMapper(CatalogsMapper.class);
        return mapper.listByFactor(m);
    }
    
    /**
     * 通过CID查询catalogs
     * @param cid
     * @return 
     */
    public Catalogs queryCatalogsByCid(Integer cid){
        CatalogsMapper mapper = this.getMapper(CatalogsMapper.class);
        return mapper.queryCatalogsByCid(cid);
    }
}
